package com.sx.wechat.command.cmd;

import com.sx.core.vo.JsonReaderResponse;
import com.sx.wechat.command.annotation.Validation;
import com.sx.wechat.command.annotation.ValidationType;
import com.sx.wechat.command.annotation.Validations;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Created by lihao on 2018/3/20.
 */
public abstract class AbstractCommand implements Command{
    protected Log logger= LogFactory.getLog(getClass());

    protected String apiPre= "a";
    protected String h5Pre= "h5";
    private static final String start="start";
    private static final String limit="limit";
    private static final String page="page";
    private static final Integer LIMIT=25;
    private static final Integer DefaultPage=1;

    protected static final String h5_render_page="h5_render_page";
    protected static final String h5_render_mode="h5_render_mode";
    @Override
    public Object execute(String prefix, HttpServletRequest request, HttpServletResponse response,Model model,MultipartFile file) {
        Validations validations = getClass().getAnnotation(Validations.class);
        if(validations!=null){
            Validation[] validation_arra = validations.value();
            for(Validation validation:validation_arra){
                ValidationType type=validation.type();
                String name=validation.name();
                String val=obtain(request,name);
                if(type==ValidationType.required && StringUtils.isEmpty(val)){
                    return new JsonReaderResponse(name+"为必须项",101);
                }
                if(type==ValidationType.number && !NumberUtils.isNumber(val)){
                    return new JsonReaderResponse(name+"为数值类型",101);
                }
            }
        }
        if(StringUtils.equals(prefix,apiPre)){
            return executeApi(request,response);
        }else if(StringUtils.equals(prefix,h5Pre)){
            return executeH5(request,response,model);
        }
        return ResponseEntity.notFound().build();
    }
    public abstract Object executeApi(HttpServletRequest request, HttpServletResponse response);

    public Object executeH5(HttpServletRequest request,HttpServletResponse response) {
        return new ModelAndView("");
    }

    public Object executeH5(HttpServletRequest request,HttpServletResponse response,Model model){
        model.addAttribute(h5_render_mode,"h5");
        return new ModelAndView("wechat_index");

    }

    public String obtain(HttpServletRequest request, String params){
        String str= request.getParameter(params);
        return str!=null?str.trim():str;
    }
    public String obtain(HttpServletRequest request, String params,String def){
        String str= request.getParameter(params);
        return str!=null?str.trim():def;
    }
    public Integer obtainLimit(HttpServletRequest request){
        String li=request.getParameter(limit);
        if(NumberUtils.isNumber(li)){
            return Integer.valueOf(li);
        }
        return  LIMIT;
    }
    public Integer obtainPage(HttpServletRequest request){
        String li=request.getParameter(page);
        if(NumberUtils.isNumber(li)){
            Integer p=Integer.valueOf(li);
            return Math.max(DefaultPage,p);
        }
        return  DefaultPage;
    }
    public Integer parseInteger(HttpServletRequest request, String filed){
        String value=request.getParameter(filed);
        if(NumberUtils.isNumber(value))
            return Integer.parseInt(value);
        return null;
    }
    public BigDecimal parseBigDecimal(HttpServletRequest request, String filed){
        String value=request.getParameter(filed);
        if(NumberUtils.isNumber(value))
            return new BigDecimal(value);
        return null;
    }

}
